package com.trafficsys.modules.loginAndRegister.controller;

import com.trafficsys.modules.entity.DriveCard;
import com.trafficsys.modules.entity.Driver;
import com.trafficsys.modules.entity.Ticket;
import com.trafficsys.modules.licenseplaterecognition.dao.TicketDao;
import com.trafficsys.modules.loginAndRegister.dao.DriveCardDao;
import com.trafficsys.modules.loginAndRegister.dao.DriverDao;
import com.trafficsys.modules.loginAndRegister.pojo.TicketAndRule;
import com.trafficsys.modules.loginAndRegister.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ExcelDownloadController {
    @Autowired
    DriverDao driverDao;
    @Autowired
    ExcelUtil excelUtil;
    /**
     * Excel表格导出接口
     * http://localhost:8080/ExcelDownload
     * response response对象
     * IOException 抛IO异常
     */
    @RequestMapping("/exportTicketsExcel")
    public void excelDownload(HttpServletResponse response) throws IOException {
        // 接收请求
        // 导出所有罚单信息
        List<TicketAndRule> cardList=driverDao.getAllTicketAndRule();
        List<List<String>> excelData=new ArrayList<>();
        List<String> header=new ArrayList<>();
        header.add("罚单号");header.add("车牌号");header.add("警员编号");header.add("处罚时间");header.add("处理时间");
        header.add("违反条例编号");header.add("违反条例");header.add("罚款数额");header.add("扣分数额");header.add("罚单状态");
        excelData.add(header);
        for (int i=0;i<cardList.size();i++){
            List<String> rowData=new ArrayList<>();
            rowData.add(cardList.get(i).getId()+"");
            rowData.add(cardList.get(i).getCarNo()+"");
            rowData.add(cardList.get(i).getPoliceId()+"");
            rowData.add(cardList.get(i).getCreateTime()+"");
            rowData.add(cardList.get(i).getUpdateTime()+"");
            rowData.add(cardList.get(i).getRuleId()+"");
            rowData.add(cardList.get(i).getRuleName()+"");
            rowData.add(cardList.get(i).getMoney()+"");
            rowData.add(cardList.get(i).getCutScore()+"");
            rowData.add(cardList.get(i).getState()+"");
            excelData.add(rowData);
        }
        //表格名
        String sheetName="驾驶证表";
        //文件名
        String fileName="driverCard.xls";
        //列宽度
        int columnWidth=10;
        //调用工具类
        excelUtil.exportExcel(response,excelData,sheetName,fileName,columnWidth);
    }
}
